package com.weiiew.versionservice.controller;

import com.weiiew.common.entity.dto.version;
import com.weiiew.common.utils.ApiResponse;
import com.weiiew.common.GatawayAccess.GatewayAccess;
import com.weiiew.versionservice.service.VersionServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 魏锴
 * @Date 2024/2/5
 */
@Slf4j
@RestController
@RequestMapping("/version")
@RequiredArgsConstructor
public class VersionController {
    private final VersionServiceImpl versionService;

    /**
     * 获得版本列表
     *
     * @return ApiResponse
     */
    @GetMapping("/list")
    public ApiResponse getVersionList() {
        return ApiResponse.ok("获取成功", "versions", versionService.getVersionList());
    }

    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    @PostMapping("/save")
    public ApiResponse saveVersion(version version) {
        versionService.saveOrUpdate(version);
        return ApiResponse.ok("修改成功", "id", version.getId());
    }

    @GatewayAccess(GatewayAccess.GatewayType.MANAGER)
    @RequestMapping("/remove/{id}")
    public ApiResponse removeVersion(@PathVariable Integer id) {
        if (versionService.remove(id)) return ApiResponse.ok("删除成功");
        return ApiResponse.failure("删除失败，该版本仍有关联数据。");
    }
}
